package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.jedis.JedisClient;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemDescExample;
import com.taotao.pojo.TbItemDescExample.Criteria;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.ItemService;

@Service
@Transactional
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Autowired
	private Destination topicDestination;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${REDIS_ITEM}")
	private String REDIS_ITEM;
	
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;

	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		PageHelper.startPage(page, rows);
		TbItemExample itemExample = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(itemExample);
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public TaotaoResult addItem(TbItem item, String desc) {
		// 持久化TbItem对象
		final long itemId = IDUtils.genItemId();
		item.setId(itemId);
		Date date = new Date();
		item.setCreated(date);
		item.setUpdated(date);
		item.setStatus((byte) 1);
		
		itemMapper.insert(item);
		
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(itemId);
		tbItemDesc.setCreated(date);
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setUpdated(date);
		
		itemDescMapper.insert(tbItemDesc);
		
		jmsTemplate.send(topicDestination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage message = session.createTextMessage(itemId+"");
				return message;
			}
		});
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteItem(String ids) {
		String[] idsArray = ids.split(",");
		for (String id : idsArray) {
			itemMapper.deleteByPrimaryKey(Long.parseLong(id));
			itemDescMapper.deleteByPrimaryKey(Long.parseLong(id));
		}
		return TaotaoResult.ok("删除成功!");
	}

	@Override
	public TaotaoResult instockItem(String ids) {
		String[] idsArray = ids.split(",");
		for (String id : idsArray) {
			TbItem item = itemMapper.selectByPrimaryKey(Long.parseLong(id));
			item.setStatus((byte) 2);
			itemMapper.updateByPrimaryKey(item);
		}
		return TaotaoResult.ok("下架成功!");
	}

	@Override
	public TaotaoResult reshelfItem(String ids) {
		String[] idsArray = ids.split(",");
		for (String id : idsArray) {
			TbItem item = itemMapper.selectByPrimaryKey(Long.parseLong(id));
			item.setStatus((byte) 1);
			itemMapper.updateByPrimaryKey(item);
		}
		return TaotaoResult.ok("上架成功!");
	}

	@Override
	public TbItem getItemById(Long itemId) {
		
		try {
			String str = jedisClient.get(REDIS_ITEM+":"+itemId+":BASE");
			if(StringUtils.isNoneBlank(str)){
				TbItem item = JsonUtils.jsonToPojo(str, TbItem.class);
				return item;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		
		try {
			String json = JsonUtils.objectToJson(item);
			jedisClient.set(REDIS_ITEM+":"+item.getId()+":BASE",json);
			jedisClient.expire(REDIS_ITEM+":"+item.getId()+":BASE", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}

	@Override
	public TbItemDesc getItemDescById(Long itemId) {
		try {
			String str = jedisClient.get(REDIS_ITEM+":"+itemId+":DESC");
			if(StringUtils.isNoneBlank(str)){
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(str, TbItemDesc.class);
				return itemDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItemDescExample example = new TbItemDescExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemDesc> itemDesc = itemDescMapper.selectByExample(example);
		if(itemDesc != null && itemDesc.size() > 0){
			try {
				String json = JsonUtils.objectToJson(itemDesc);
				jedisClient.set(REDIS_ITEM+":"+itemDesc.get(0).getItemId()+":DESC",json);
				jedisClient.expire(REDIS_ITEM+":"+itemDesc.get(0).getItemId()+":DESC", REDIS_ITEM_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return itemDesc.get(0);
		}
		return null;
	}
}
